//
//  Emoticons.swift
//  Emoticon
//
//  Created by 刘俊 on 15/10/27.
//  Copyright © 2015年 刘俊. All rights reserved.
//

import UIKit

///表情包数组
class Emoticons: NSObject {

    ///表情包地址
    var id: String?
    ///显示在toolbar上的名字
    var group_name_cn: String?
    ///表情包数组
    lazy var emoticons = [Emoticon]()
    
    init(var dict: [String: AnyObject]) {
        super.init()
        
        id = dict["id"] as? String
        group_name_cn = dict["group_name_cn"] as? String
        
        var index = 0 //判断是否插入删除按钮
        if let Array = dict["emoticons"] as? [[String: AnyObject]] {
            for var d in Array {
                //修改png属性的值
                if let png = d["png"] as? String,dir = id {
                    d["png"] = dir + "/" + png
                }
                emoticons.append(Emoticon(dict: d))
                index++
                if index == 20 {
                    emoticons.append(Emoticon(isRemove: true))
                    index = 0
                }

            }
        }
        appendEmptyEmoticon()
        
    }
    /// 当一个分组没有满时插入空白
    private func appendEmptyEmoticon() {
        let count = emoticons.count % 21
        if count == 0 && emoticons.count > 0 {
            return
        }
        //添加空白表情
        for _ in count..<20 {
            emoticons.append(Emoticon(isEmpty: true))
        }
        //最后一个添加删除按钮
        emoticons.append(Emoticon(isRemove: true))
        
    }
    override var description: String {
        let keys = ["id","group_name_cn","emoticons"]
        return dictionaryWithValuesForKeys(keys).description
    }
}
